Skip to content

Validate e2e consensus traces against the Lean model in CI - #8732

Merged
cjen1-msft merged 62 commits into
mainfrom
cjen1-msft-laughing-memory
Nov 26, 2026
Merged

cjen1-msft merged 62 commits into
mainfrom
cjen1-msft-laughing-memory

Conversation

@cjen1-msft

@cjen1-msft cjen1-msft commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

The Lean model of the CCF consensus protocol is only checked against cft_driver scenarios, which run every node in one process on a scripted schedule. This PR checks it against real networks. Every e2e test now records its nodes' trace events, and a new CI job replays the ccf.cft events of each capture against the model, failing if the implementation takes a step the model does not allow. The existing e2e tests, including reconfiguration, elections and partitions, become a conformance check between the model and the implementation on every PR.

A capture need not be complete: a test may kill a node before it sends its last events. The replayer therefore gains --lenient, which stops where it can go no further instead of failing, and reports a watermark: the least fraction of a node's events that it consumed. CI requires a watermark of at least 90% for every capture. A step the model disagrees with still fails the capture outright.

The capture is not specific to consensus: it holds the events of every tag, each with its tag, so other replayers can use it later.

Performance optimisations of the replayer are left to a PR stacked on this one.

What is not validated

What Tests Why
Networks that recover a service The recovered networks in recovery.py, recovery_snapshot_endorsements.py, identity_recovery_snapshot.py, and parts of e2e_operations.py, governance.py and partitions_test.py; the networks they recover from are validated Not recorded: replay bootstraps from a fresh genesis. Left to a follow-up PR
Opted out with NO_TRACE_CAPTURE lts_compatibility Released nodes do not support the capture sink
Opted out with NO_TRACE_CAPTURE cft_trace_e2e It tests exact sink configurations
Benchmarks (LABEL perf) logging_cert_blocking_locust, basic_js_locust, basic_blocking_locust, fluentd, logging_jwt_blocking_locust, historical_query_locust They expect their own collector to be the nodes' only sink
Recorded and uploaded, but not replayed Tests that run only outside the ci.yml Virtual jobs: the SNP job, and the coverage.yml, long-test.yml and release.yml workflows (for example regression_test_suite) The replay job only downloads the Virtual jobs' logs
Captures without ccf.cft events Networks whose first node never starts, as in some configuration checks Nothing to replay
How it works and how to use it
  • add_e2e_test passes --capture-trace to every test, unless it sets NO_TRACE_CAPTURE or is a benchmark.
  • Each Network starts one local Fluentd Collector when it first configures a node, and gives each node an extra sink that accepts every tag, keeping any sinks the test configured.
  • The collector's callback appends events to <workspace>/<label>_trace/<n>.ndjson as they arrive, one {tag, ts, pid, m_ts, msg} record per line from every node, where ts is the event's Fluentd timestamp in nanoseconds. Networks that share a label get separate <n> files, and a test process removes an earlier run's captures the first time it uses a trace folder.
  • stop_all_nodes stops the collector. If the collector failed, it discards the capture and logs Discarded trace capture with the reason.
  • Collector in tests/infra/fluentd.py gains an optional host, as IPv6-only networks must not have IPv4 addresses in their node configs. Its thread becomes a daemon, so a network that is never stopped cannot keep the test process alive. It replaces bytes that are not UTF-8 instead of failing, as log messages can contain them.
  • consensus-replay ignores records whose tag is not ccf.cft. With --lenient, a node's groups end before its first event that no rule consumes, and replay ends once no group may run. The output gains watermark, which is 1 without --lenient.
  • In CI, the Virtual jobs upload captures in their logs-* artifacts. The Replay e2e consensus traces job runs after them, whether or not their tests passed, unless the run was cancelled. It builds consensus-replay, or restores it from a cache keyed on the Lean sources, and runs tests/cft_trace_validation.py over their logs. It fails if the model disagrees with a capture, if a watermark is below 90%, or if there is no capture.
  • Locally: python3 tests/cft_trace_validation.py <consensus-replay> build/workspace.
  • Most of the stop_all_nodes diff is re-indentation for a try/finally; review it with whitespace hidden.

Alternatives

  • Replay in each test job, after its tests: Virtual C and the SNP job already install Lean, but Virtual A and B do not, and would each need the toolchain and a replayer build. For now replay stays in its own job, which downloads the captures the test jobs upload.
  • Replay only captures known to be complete (every node exited cleanly, reported no dropped events and closed its connection): this needs the capture held until the network stops and several checks of node logs, and skips every test that kills a node. The cost of the watermark is that a disagreement in the unconsumed tail of a capture goes unseen.
  • Opt in per test: coverage would depend on remembering the argument for each new test, so capture is on by default with an explicit opt-out.

@cjen1-msft
cjen1-msft force-pushed the cjen1-msft-laughing-memory branch from 74c28e9 to 973e0e8 Compare November 10, 2026 17:16
@cjen1-msft
cjen1-msft changed the base branch from main to cjen1-msft-symmetrical-bassoon November 16, 2026 15:47
@cjen1-msft
cjen1-msft force-pushed the cjen1-msft-laughing-memory branch 2 times, most recently from 6d42d75 to 68ef415 Compare November 16, 2026 16:08
@cjen1-msft cjen1-msft changed the title Add per-network ccf.cft trace capture to e2e tests Validate e2e consensus traces against the Lean model in CI Nov 16, 2026
@cjen1-msft
cjen1-msft force-pushed the cjen1-msft-laughing-memory branch from 68ef415 to 2578f17 Compare November 18, 2026 10:21
@cjen1-msft
cjen1-msft added this pull request to stack #8730 November 18, 2026 14:25
@cjen1-msft
cjen1-msft force-pushed the cjen1-msft-laughing-memory branch from 2578f17 to 0779d6c Compare November 18, 2026 15:58
cjen1-msft and others added 16 commits November 18, 2026 17:04
Replay takes one NDJSON file of {process_id, h_ts, msg} records from all
nodes in any order. It orders each process's records by h_ts, splits them
into per-node streams, and reduces each stream into groups by grammar
alone. A scheduler repeatedly runs, atomically, the next group of any node
whose group is enabled, preferring the earliest h_ts. Receives wait for a
matching queued packet and init_as_backup for a committed source; replay
fails if groups remain but none is enabled, listing the waiting groups.

Fixtures gain process_id and h_ts. The interleaved control records the
joiner's events among the leader's, including a same-name event inside
the leader's group, receives before their sends, and initialization
before the leader commits.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
check-order.py replays each trace after shuffling its records, and after
also moving each node into its own process with a random, gapped h_ts
counter, which changes the order in which enabled groups run. CI runs it
on the fixtures and on the cft_scenario_test captures.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Each node's events are still reduced in h_ts order, which is the order of
its lock holds. Replay now treats h_ts as the order of execution within a
process, prefers the earliest wall-clock time among processes, and waits
only for a group that is not enabled, so clock skew cannot fail a trace.
Snapshot hooks, which run outside the lock, no longer absorb the groups
that fall before the initialization, and bootstrap accepts a nomination
before genesis.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Reduction.lean now only defines the rules, each consuming one lock hold of
a node. Groups.lean peeks a node's next event, runs one rule from it and
takes what the rule emitted as a group, so the rules track neither the
group's node nor where it starts and ends.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
A snapshot hook is valid if its node initializes later, which the hook rule
can read from the node's remaining events, so the rules no longer track
whether the node is initialized or a hook is pending.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The snapshot hooks stay in the init_as_backup rule, which now stops after
them when the initialization does not directly follow and requires it
later in the node's events. The rules otherwise differ from main only in
accepting a nomination before genesis.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
A group that waits is retried after every other group that runs. Its
reason rendered and sorted the whole pending queue between its sender and
receiver each time, and was discarded whenever another group could run.
The waiting check now returns the reason as a thunk, forced only by the
stuck report.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Snapshot hooks run outside the consensus lock, so they are not part of the
initialization's lock hold even when it directly follows them in the
node's events. Grouping them made the group start at the hook, and another
node's group between hook and initialization in the same process was then
rejected as overlapping. Hooks now never emit, so the group starts at
init_as_backup.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The fixtures and captures exercise input ordering and scheduling through
the replayer itself, so the hand-built groups and the replay wrapper they
needed are removed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Exported records carry pid instead of process_id, and the consensus
replayer, its fixtures and the capture tests follow.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The per-process counter in exported records is monotonic, unlike the
event time, so it is named m_ts instead of h_ts. The consensus replayer,
its fixtures and the capture tests follow.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The step rule now tries bootstrap like any other rule, at a node's first
event, and records the bootstrapping node in the rule state, so the driver
only repeats the step rule.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Events and groups carry m_ts rather than time, and each field says which
trace field it comes from.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Real-node traces, which exercise the cross-process scheduler directly, are
to be replayed in CI by a follow-up, so the script and its two workflow
steps are removed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Groups were queued per process, with overlapping groups rejected, so that
a trace edited into an order one thread could not have produced was
refused. A real capture cannot have that order, and the model already
rejects a group it is not ready for, so each node's groups now form their
own queue.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@cjen1-msft
cjen1-msft force-pushed the cjen1-msft-laughing-memory branch from 0779d6c to cf29400 Compare November 18, 2026 17:04
cjen1-msft and others added 2 commits November 18, 2026 17:13
Add --capture-cft-trace to the e2e arguments. When set, each Network
lazily starts a recording Fluentd collector when it first configures a
node, appends a ccf.cft-only sink to a per-node copy of the
observability configuration, and on stop_all_nodes (in a finally)
writes <label>_trace/<n>/cft.ndjson with a manifest. The capture is
complete only if every configured node, including retired ones, exited
cleanly and reported no dropped events for that sink, and every
connection reached EOF; otherwise it is inconclusive, which never fails
the test. Nodes from released versions are rejected.

Enable it for e2e_forwarding_smoke so CI exercises the capture path.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Exit only after an idle select that started after stop, so a
connection still queued in the listen backlog is accepted and read
rather than lost from a capture reported as complete. Spell out the
capture path and manifest in the argument help.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 22:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The replay job’s dependency conflicts with the PR description’s scheduling and rerun guidance, so the description or implementation needs to be aligned.

1 open issue

Review effort: Balanced

Comment thread .github/workflows/ci.yml Resolved
@achamayou

Copy link
Copy Markdown
Member

Pushed two follow-up commits to reduce CI runner time and speed up Replay e2e consensus traces:

  • bc62ec8a7: Replaced the sleep 30 polling loop in cft_replay with needs: [vmss-virtual-a, vmss-virtual-b, vmss-virtual-c] and if: ${{ !cancelled() }}. The polling loop held an ubuntu-26.04 runner idle for 13–20 minutes per run to overlap ~23s of Lean setup while adding 6–29s of poll lag; using needs: removes the idle runner time and lets re-running a failed Virtual job automatically re-trigger replay.
  • 4fb00c2b0: Sped up consensus-replay and cached its binary in CI (on the 138 captures from run 45454378454: 4-worker wall time dropped from 164.7s to 25.0s, total CPU from 644s to 99.8s, and peak RSS on the 327 MiB reconfiguration_nodes trace from 3.0 GB to 574 MB, with 0 output differences):
    1. Streaming line reader + msg deduplication (Input.lean): 97.7% of events are bare ticks. Instead of reading the entire file into memory, running String.splitOn "\n", and Json.parse-ing every line (including non-ccf.cft tags), readEvents streams via Handle.getLine, skips non-ccf.cft lines before parsing, and reuses the parsed Json object for consecutive byte-identical msg payloads per pid (falling back to full Json.parse on any mismatch).
    2. Per-node observation cache (Interpreter.lean): observeState previously ran 5.94M times (44× more than actions) and walked state.log ~10 times per call (length, zipIdx, activeConfigurations, and retirement ×4). It now derives all observed fields in a single pass over state.log, caches them per node until an action steps that node, and short-circuits repeated identical snapshots in O(1).
    3. Hot-loop & scheduling cleanup (Input.lean, Reduction.lean, Groups.lean, Interpreter.lean): Bucketed events by pid and sorted by m_ts with Array.qsort instead of List.mergeSort on strings; replaced 8 backtracking attempt rules before ruleTick with direct match head.function; pre-indexed Header.nodeIds in a HashMap; added a zero-allocation min-queue scan in schedule, inline action/observation counts, and skipped recording the schedule array unless --schedule is passed.
    4. Parallel dispatch & CI binary cache (tests/cft_trace_validation.py, .github/workflows/ci.yml): Dispatched captures largest-first while keeping deterministic alphabetical output, and cached the self-contained consensus-replay binary on lean/lean-toolchain, lean/common/**, and lean/consensus/** to skip Lean install/build on non-Lean PRs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 09:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Coupled changes to replay invariants, lenient scheduling, and observation caching require domain review and validation with Lean fixtures and real e2e traces, which could not be performed here.

1 open issue

Review effort: Balanced

@achamayou

Copy link
Copy Markdown
Member
gantt
    title PR #8732 Elapsed Timeline (T+00:00 = 09:04:43Z, T+16:53 = 09:21:36Z)
    dateFormat HH:mm:ss
    axisFormat %M:%S
    todayMarker off

    section ci.yml
    Virtual A (queued 11s - ci pool)              :done, ci_aq, 00:00:01, 00:00:12
    Virtual A (run 12m15s)                        :ci_ar, 00:00:12, 00:12:27
    Virtual C (queued 1m19s - ci-c pool)          :done, ci_cq, 00:00:01, 00:01:20
    Virtual C (run 10m04s)                        :ci_cr, 00:01:20, 00:11:24
    ACI SNP Genoa (queued 2m28s)                  :done, ci_sq, 00:00:01, 00:02:29
    ACI SNP Genoa (run 9m44s)                     :ci_sr, 00:02:29, 00:12:13
    Virtual B (queued 5m15s - ci-b pool)          :crit, done, ci_bq, 00:00:01, 00:05:16
    Virtual B (run 10m29s - critical path)        :crit, ci_br, 00:05:16, 00:15:45
    Replay e2e CFT traces (needs A+B+C, 1m03s)    :crit, ci_rep, 00:15:50, 00:16:53

    section cross-platform-lts.yml
    Configure LTS (5s)                            :lts_c, 00:00:04, 00:00:09
    Build AL3 7.0.28 (queued 1m42s - ci pool)     :done, lts_3q, 00:00:09, 00:01:51
    Build AL3 7.0.28 (run 6m11s)                  :lts_3r, 00:01:51, 00:08:02
    Build AL4 HEAD (queued 3m48s - ci pool)       :done, lts_4q, 00:00:09, 00:03:57
    Build AL4 HEAD (run 4m15s)                    :lts_4r, 00:03:57, 00:08:12
    Upgrade AL3->AL4 (queued 14s, needs builds)   :done, lts_uq, 00:08:13, 00:08:27
    Upgrade AL3->AL4 (run 1m53s)                  :lts_ur, 00:08:27, 00:10:20

    section lean.yml (+ lean-package.yml)
    Check common (3m50s)                          :lp_com, 00:00:05, 00:03:55
    Check kv (3m34s)                              :lp_kv, 00:00:05, 00:03:39
    Check consistency (3m35s)                     :lp_cs, 00:00:04, 00:03:39
    Check disaster-recovery (4m38s)               :lp_dr, 00:00:05, 00:04:43
    Check consensus (7m08s - blocks gatekeeper)   :active, lp_con, 00:00:04, 00:07:12
    CFT scenario captures (ci-c, 3m11s)           :l_cft, 00:00:10, 00:03:21
    Replay native CFT (needs cft_traces, 38s)     :l_cftr, 00:03:24, 00:04:02
    KV trace captures (queued 1m10s - ci-c)       :done, l_kvq, 00:00:01, 00:01:11
    KV trace captures (run 2m56s)                 :l_kvr0, 00:01:11, 00:04:07
    Replay KV traces (needs kv_traces, 34s)       :l_kvr, 00:04:10, 00:04:44
    Consistency capture+replay (ci-c, 4m22s)      :l_cs, 00:00:11, 00:04:33
    Lean models and proofs (needs all, 5s)        :active, l_mod, 00:07:15, 00:07:20

    section documentation.yml & Copilot
    Build documentation (3m30s)                   :doc_b, 00:00:05, 00:03:35
    Deploy (skipped on PR)                        :milestone, doc_d, 00:03:36, 0s
    Copilot PR reviewer (3m37s)                   :cop_r, 00:00:16, 00:03:53
Loading

@achamayou

Copy link
Copy Markdown
Member

cjen1-msft it may make sense to move a bit of B to C if we can, to make up for the extra minute of TV at the end, but it's a very reasonable cost to pay considering the value.

Restore the replayer and the validator script to their state before
4fb00c2, keeping that commit's replayer binary cache and the job
dependency from bc62ec8 in the cft_replay CI job.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 15:56
@cjen1-msft

Copy link
Copy Markdown
Contributor Author

Amaury Chamayou (@achamayou) I've reverted some of the changes you proposed.
I think that making this PR minimal - turning on trace validation in the CI. At the cost of ~1min longer CI is an ok tradeoff (we shouldn't make it required until we're happy its not flakey).

Then I think we should stack your performance improvement fixes on top in a separate PR, as I think there is some digging we should do to hit a good point on the complexity vs performance curve.
But I do agree with the aim of making this as cheap to run in CI as possible.

Copilot stopped reviewing on behalf of cjen1-msft due to an error November 25, 2026 16:07
@achamayou

Copy link
Copy Markdown
Member

I think that making this PR minimal - turning on trace validation in the CI. At the cost of ~1min longer CI is an ok tradeoff (we shouldn't make it required until we're happy its not flakey).

This is already adding 1 minute to the critical path, I am not happy with adding another minute we don't need.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The lenient reducer and scheduler change consensus-conformance coverage, requiring human review of truncation/watermark semantics and the per-node 90% threshold because Lean fixture and real e2e replay validation were unavailable.

1 open issue

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 26, 2026 14:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Lenient replay changes the consensus-conformance boundary, and unavailable Lean/network validation leaves truncation handling and the 90% trace watermark needing human review against representative real-network CI captures.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@cjen1-msft
cjen1-msft added this pull request to the merge queue Nov 26, 2026
Merged via the queue into main with commit 131dde2 Nov 26, 2026
31 checks passed
@cjen1-msft
cjen1-msft deleted the cjen1-msft-laughing-memory branch November 26, 2026 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants